import { defineStore } from "pinia";
import { reactive, ref } from "vue";
import { reqLoginAPI } from '@/apis/user'
import { testExample } from "@/apis/test.js";
export const useUserStore = defineStore('user', () => {
  const appBaseInfo = ref({});
  const userInfo = ref(null);

  // 获取 app 信息
  const getAppBaseInfo = async () => {
    try {
      const res = await testExample();
      if (res.code == 1) {
        appBaseInfo.value = res.data;
      }
    } catch (error) {
      appBaseInfo.value = reactive({
        error: "暂无app信息"
      })
    }
  }
  // 获取用户信息
  const userLogin = async () => {
    try {
      const res = await reqLoginAPI();
      if (res.code == 1) {
        userInfo.value = res.data;
      }
    } catch (error) {
      userInfo.value = {
        name: "游客登录"
      }
      console.error('登录失败:', error);
    }
  }
  return { userInfo, userLogin, appBaseInfo, getAppBaseInfo };
},
  {
    // pinia 数据持久
    persist: {
      getItem: (key) => uni.getStorageSync(key),
      setItem: (key, value) => uni.setStorageSync(key, value)
    }
  }
)